package adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;

import java.math.BigDecimal;
import java.util.List;

import bean.GasStationBean;
import zhang.automobileservice.R;
import zhang.automobileservice.ShowMapActivity;


/**
 * Created by Administrator on 2016/7/21.
 */
public class GasStationAdapter extends BaseAdapter {
    private List<GasStationBean> strList;
    private Context context;
    private LatLonPoint zuoBiao;

    public GasStationAdapter(Context context, List<GasStationBean> strList) {
        this.strList = strList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return strList.size();
    }

    @Override
    public Object getItem(int position) {
        return strList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_gasstation_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final GasStationBean gasStationBean = strList.get(position);
        viewHolder.name.setText(gasStationBean.getName());
        int distance = Integer.valueOf(gasStationBean.getDistance());
        if(distance<1000){
            viewHolder.distance.setText(distance+"m");
        }else {
            BigDecimal b = new BigDecimal(distance/(double)1000);
            viewHolder.distance.setText(b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue()+"km");
        }
        viewHolder.location.setText(gasStationBean.getLocation());
        viewHolder.map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zuoBiao = gasStationBean.getZuoBiao();
                Intent intent = new Intent(context,ShowMapActivity.class);
                intent.putExtra("latitude",zuoBiao.getLatitude());
                intent.putExtra("longitude",zuoBiao.getLongitude());
                intent.putExtra("title",gasStationBean.getName());
                context.startActivity(intent);
            }
        });
        viewHolder.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("android.intent.action.CALL");//指定动作名称，这个只在上面的Activity中找 Intent.ACTION_CALL;
                intent.setData(Uri.parse("tel:"+gasStationBean.getPhone()));//传递号码，tel:实在上面Activity中定义的
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView name;
        public TextView distance;
        public TextView location;
        public LinearLayout map,ll_visiable;
        public RelativeLayout phone;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.name = (TextView) rootView.findViewById(R.id.name);
            this.distance = (TextView) rootView.findViewById(R.id.distance);
            this.location = (TextView) rootView.findViewById(R.id.location);
            this.phone = (RelativeLayout) rootView.findViewById(R.id.phone);
            this.map = (LinearLayout) rootView.findViewById(R.id.map);
            this.ll_visiable = (LinearLayout) rootView.findViewById(R.id.ll_visiable);
        }

    }

}


